package com.yg.controller;


import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yg.config.result.R;
import com.yg.entity.singleEntity.QualityCourse;
import com.yg.entity.singleEntity.QualityProfessional;
import com.yg.service.QualityProfessionalService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 对应sheet06，此表是质量工程专业表，对应表中的专业部分 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/qualityProfessional")
@CrossOrigin
@Api(tags = "对应第六张表")
public class QualityProfessionalController {

    @Autowired
    private QualityProfessionalService qualityProfessionalService;

    /**
     * 增加质量工程专业数据
     *
     * @param qualityProfessional
     * @return
     */
    @ApiOperation("增加质量工程专业数据")
    @PostMapping("postProfessional")
    private R postQualityProfessional(
            @ApiParam(name = "质量工程专业数据") @RequestBody(required = true) QualityProfessional qualityProfessional
    ) {

        try {
            boolean flag = qualityProfessionalService.save(qualityProfessional);
            return flag ? R.OK() : R.ERROR();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("添加失败");
        }
    }

    /**
     * 获取所有的质量工程专业数据
     *
     * @return
     */
    @ApiOperation("获取所有的质量工程专业数据")
    @GetMapping("getAllProfessional")
    private R getAllQualityProfessional(
            @ApiParam(name = "current", value = "当前页", required = true) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = true) @RequestParam(name = "limit") long limit
    ) {

        try {
            Page<QualityProfessional> page = new Page<>(current, limit);
            QueryWrapper<QualityProfessional> wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);
            qualityProfessionalService.page(page, wrapper);
            List<QualityProfessional> list = page.getRecords();
            long total = page.getTotal();

            long pages = page.getPages();

            return R.OK().data("qualityProfessionalList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }
    }

    /**
     * 根据条件获取质量工程专业数据
     *
     * @return
     */
    @ApiOperation("根据条件获取质量工程专业数据")
    @PostMapping("selectProfessionalByCondition")
    private R selectQualityProfessionalByCondition(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "qualityProfessional", value = "条件", required = false) @RequestBody(required = false) QualityProfessional qualityProfessional
    ) {
        try {
            Page<QualityProfessional> page = new Page<>(current, limit);

            QueryWrapper<QualityProfessional> wrapper = null;
            wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);
            if (!StringUtils.isEmpty(qualityProfessional.getName())) {
                wrapper.like("name", qualityProfessional.getName());
            }

            if (!StringUtils.isEmpty(qualityProfessional.getLevel())) {
                wrapper.eq("level", qualityProfessional.getLevel());
            }

            qualityProfessionalService.page(page, wrapper);

            List<QualityProfessional> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            return R.OK().data("qualityProfessional", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }
    }

    /**
     * 修改质量工程专业数据
     *
     * @return
     */
    @ApiOperation("修改质量工程专业数据")
    @PutMapping("putProfessional")
    private R putQualityProfessional(
            @ApiParam(name = "qualityProfessional", value = "修改后的专业数据", required = false) @RequestBody QualityProfessional qualityProfessional
    ) {
        QueryWrapper<QualityProfessional> wrapper = new QueryWrapper<>();
        wrapper.eq("id", qualityProfessional.getId());
        boolean flag = qualityProfessionalService.update(qualityProfessional, wrapper);
        return flag ? R.OK() : R.ERROR().message("修改失败");
    }

    /**
     * 删除质量工程专业数据
     *
     * @return
     */
    @ApiOperation("删除质量工程专业数据")
    @DeleteMapping("deleteProfessional")
    private R deleteQualityProfessional(
            @ApiParam(name = "qualityProfessional", value = "专业数据", required = false) @RequestBody QualityProfessional qualityProfessional
    ) {
        String id = null;
        try {
            id = qualityProfessional.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询不到此信息");
        }
        try {
            boolean flag = qualityProfessionalService.removeById(id);
            return flag ? R.OK() : R.ERROR();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("删除失败");
        }
    }
}

